package com.wonders.frame.autoGeneration.support.enums;

import com.google.common.util.concurrent.MoreExecutors;
import com.wonders.frame.autoGeneration.task.Core.Task;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;
import java.util.concurrent.*;

public enum ExecutorServiceEnum {
    INSTANCE;

    private ExecutorService executorService;

    ExecutorServiceEnum() {
        int cpu = Runtime.getRuntime().availableProcessors();
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(cpu);
        this.executorService = MoreExecutors
                .listeningDecorator(new ThreadPoolExecutor(cpu + 1, 2 * cpu + 1, 0L,
                        TimeUnit.MILLISECONDS, queue,
                        (r, e) -> {
                            throw new RejectedExecutionException("Task " + r.toString() +
                                    " rejected from " +
                                    e.toString());
                        }));
    }

    public static void execute(List<Task> tasks) throws InterruptedException {
        INSTANCE.executorService.invokeAll(tasks);
    }

}
